import localforage from "localforage";

export function removeItemInArray(item, items) {
    return items.filter(function (fns) {
        return !isEqual(fns, item);
    });
}

export function isEqual(a, b) {
    return a === b;
}

export function isFun(any) {
    return typeof any === 'function';
}


export function isArray(any) {
    return any instanceof Array;
}

export function isNull(any) {
    return any == void 0;
}

export function debounce(fn, delay) {
    let timer;
    return function (...params) {
        let context = this;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, params);
        }, delay);
    };
}

export async function saveToStorage(key, data) {
    // 转成字符串压缩并存储，或者转成其余约定压缩格式
    let oldData = await localforage.getItem(key);
    oldData = oldData && JSON.parse(oldData) || [];
    console.log(oldData, "存储前数据", data);
    let newArr = oldData.concat(data);
    console.log('存储后数据长度', oldData.concat(data).length);
    let newData = JSON.stringify(newArr);
    return localforage.setItem(key, newData);
}

export function getReplayData(key) {
    return localforage.getItem(key).then((data) => data ? JSON.parse(data) : []);
}

export function isDebounceType(type) {
    return !['mousemove', 'scroll', 'wheel'].every(t => type !== t);
}